#include "translationrulesform.h"
#include "ui_translationrulesform.h"
#include "translationrules.h"
#include "skiprulesmodel.h"
#include "skiprulesdelegate.h"
#include "replacerulesmodel.h"
#include "replacerulesdelegate.h"
/**
  *\fn TranslationRulesForm::TranslationRulesForm(QWidget *parent)
  *\brief Constructor
  *\param parent form's parent
  */
TranslationRulesForm::TranslationRulesForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TranslationRulesForm)
{
    ui->setupUi(this);

    m_skipRulesDelegate = new SkipRulesDelegate(this);
    m_replaceRulesDelegate = new ReplaceRulesDelegate(this);
    ui->replaceTableView->setItemDelegate(m_replaceRulesDelegate);
    ui->skipTableView->setItemDelegate(m_skipRulesDelegate);
    connect(ui->nameEdit,SIGNAL(textEdited(QString)),this,SLOT(onNameChanged(QString)));
    connect(ui->skippedListEdit,SIGNAL(textEdited(QString)),this,SLOT(onSkipedListChanged(QString)));
    connect(ui->skipNewButton,SIGNAL(clicked()),this,SLOT(addSkipRule()));
    connect(ui->skipRemoveButton,SIGNAL(clicked()),this,SLOT(removeSkipRule()));
    connect(ui->replaceNewButton,SIGNAL(clicked()),this,SLOT(addReplaceRule()));
    connect(ui->replaceRemoveButton,SIGNAL(clicked()),this,SLOT(removeReplaceRule()));
    connect(ui->replaceUpButton,SIGNAL(clicked()),this,SLOT(moveUpReplaceRule()));
    connect(ui->replaceDownButton,SIGNAL(clicked()),this,SLOT(moveDownReplaceRule()));
}

/**
  *\fn TranslationRulesForm::~TranslationRulesForm()
  *\brief Destructor
  */
TranslationRulesForm::~TranslationRulesForm()
{
    delete ui;
}

/**
  *\fn TranslationRulesForm::setTranslationRules(TranslationRules *rules)
  *\brief Sets the TranslationRules object edited by the form
  */
void TranslationRulesForm::setTranslationRules(TranslationRules *rules){
    m_rules = rules;
    /*Initializing form's nameEdit text*/
    ui->nameEdit->setText(m_rules->objectName());

    /*Initializing form's skipedLinesEdit text*/
    QString skipList = "";
    /*Build the string containing skiped lines list*/
    foreach(int line,m_rules->m_skipList){
        skipList+=QString::number(line);
        skipList+=",";
    }
    /* Removing last comma */
    int len = skipList.length()-2;
    skipList.truncate(len);
    ui->skippedListEdit->setText(skipList);

    ui->replaceTableView->setModel(m_rules->m_replaceRulesModel);
    ui->skipTableView->setModel(m_rules->m_skipRulesModel);
}

/**
  *\fn TranslationRulesForm::onNameChanged(const QString& name)
  *\brief Sets the TranslationRules object name
  * Slot is triggered by editing the object name in the form's nameEdit.
  */
void TranslationRulesForm::onNameChanged(const QString& name){
    if(m_rules)
        m_rules->setName(name);
}

/**
  *\fn TranslationRulesForm::onSkipedListChanged(const QString& skipedList)
  *\brief Sets the TranslationRules skiped lines list
  * Slot is triggered by editing the skiped lines list in the form's skipedListEdit.
  */
void TranslationRulesForm::onSkipedListChanged(const QString& skipedList){
    if(m_rules){
        QStringList list = skipedList.split(",",QString::SkipEmptyParts);
        m_rules->m_skipList.clear();
        foreach(QString str, list){
            m_rules->m_skipList.append(str.toInt());
        }
    }
}

void TranslationRulesForm::addSkipRule(){
    if(m_rules){
        int row = m_rules->m_skipRulesModel->rowCount(QModelIndex());
        m_rules->m_skipRulesModel->insertRow(row);
    }
}

void TranslationRulesForm::removeSkipRule(){
    if(m_rules){
        QModelIndex mi = ui->skipTableView->currentIndex();
        if(mi.isValid())
            m_rules->m_skipRulesModel->removeRow(mi.row());
    }
}

void TranslationRulesForm::addReplaceRule(){
    if(m_rules){
        int row = m_rules->m_replaceRulesModel->rowCount(QModelIndex());
        m_rules->m_replaceRulesModel->insertRow(row);
    }
}

void TranslationRulesForm::removeReplaceRule(){
    if(m_rules){
        QModelIndex mi = ui->replaceTableView->currentIndex();
        if(mi.isValid())
            m_rules->m_replaceRulesModel->removeRow(mi.row());
    }
}

void TranslationRulesForm::moveUpReplaceRule(){
    if(m_rules){
        QModelIndex mi = ui->replaceTableView->currentIndex();
        if(mi.isValid()){
            int row = mi.row();
            m_rules->m_replaceRulesModel->moveUp(row);
        }
    }
}

void TranslationRulesForm::moveDownReplaceRule(){
    if(m_rules){
        QModelIndex mi = ui->replaceTableView->currentIndex();
        if(mi.isValid()){
            int row = mi.row();
            m_rules->m_replaceRulesModel->moveDown(row);
        }
    }
}

void TranslationRulesForm::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
